package com.wishu.finance.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wishu.finance.domain.*;
import com.wishu.finance.service.TFkBusinessCreditService;
import com.wishu.finance.service.TJrProductService;
import com.wishu.finance.service.TJrRentInstallmentService;
import com.wishu.finance.service.TbConfigUserBusinessCircleService;
import com.wishu.finance.utils.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("finance")
public class FinanceController {

    @Resource
    private TJrRentInstallmentService rentInstallmentService;
    @Resource
    TJrProductService jrProductService;
    @Resource
    TFkBusinessCreditService fkBusinessCreditService;
    @Resource
    TbConfigUserBusinessCircleService tbConfigUserBusinessCircleService;

    /**
     * 租房合同 列表
     *
     * @param page
     * @param tJrRentInstallment
     * @return
     */
    @RequestMapping("lease_contract_list")
    public Map<String, Object> leaseContractList(Page page, TJrRentInstallment tJrRentInstallment) {
        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<TJrRentInstallment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtils
                        .isNotEmpty
                                (tJrRentInstallment.getRentInstallmentOrderNo())
                , TJrRentInstallment::getRentInstallmentOrderNo
                , tJrRentInstallment.getRentInstallmentOrderNo());
        Page list = rentInstallmentService.page(page, wrapper);
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", list);
        return map;
    }

    /**
     * 租房合同 - 详情
     *
     * @param contractId
     * @return
     */
    @RequestMapping("detail")
    public Map<String, Object> detail(Integer contractId) {
        Map<String, Object> map = new HashMap<>();
        ContractDetailsResponseDTO contract = rentInstallmentService.getContract(contractId);
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", contract);
        return map;
    }

    /**
     * @return R
     * @Description: 列表 - 金融产品
     */
    @RequestMapping("list_product")
    public Map<String, Object> listProduct() {
        HashMap<String, Object> map = new HashMap<>();
        List<ProductResponseDTO> listProduct = rentInstallmentService.listProduct();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", listProduct);
        return map;
    }

    /**
     * @return R
     * @Description: 校验 - 金融产品
     */
    @RequestMapping("check_product")
    public Map<String, Object> checkProduct(Long productId, Long periods) {
        HashMap<String, Object> map = new HashMap<>();
        CheckProductRequestBeanDTO dto = new CheckProductRequestBeanDTO();
        dto.setPeriods(periods);
        dto.setProductId(productId);
        boolean checkProduct = rentInstallmentService.checkProduct(dto);
        if (checkProduct) {
            map.put("code", 200);
        } else {
            map.put("code", 777);
        }
        return map;
    }


    /**
     * @return ModelAndView
     * @Description: 计算 - 列表
     */
    @RequestMapping("list_compute")
    public Map<String, Object> listCompute(@RequestBody ComputeRentInstallmentDetailRequestBean bean) {
        SearchRentInstallmentDetailRequestDTO dto = new SearchRentInstallmentDetailRequestDTO();
        HashMap<String, Object> map = new HashMap<>();
        BeanUtils.copy(bean, dto);
        Date s = new Date();
        Date e = new Date();
        s.setTime(Long.parseLong(bean.getStartDate()));
        e.setTime(Long.parseLong(bean.getEndDate()));
        dto.setEndDate(e);
        dto.setStartDate(s);
        dto.setBalance(Double.parseDouble(bean.getBalance()));
        SearchRentInstallmentDetailResponseDTO r = rentInstallmentService.getRentInstallmentDetail(dto);
        map.put("code", 200);
        map.put("data", r);
        map.put("msg", "查询成功");
        return map;
    }

    /**
     * @return ModelAndView
     * @Description: 保存 - 分期申请
     */
    @RequestMapping("save")
    public Map<String, Object> save(@RequestBody SaveRentInstallmentForm saveForm) {
        HashMap<String, Object> map = new HashMap<>();
        CheckProductRequestBeanDTO checkdto = new CheckProductRequestBeanDTO();
        checkdto.setPeriods(Long.valueOf(saveForm.getPeriods()));
        checkdto.setProductId(Long.valueOf(saveForm.getProductId()));

        Integer a = Integer.valueOf(saveForm.getPeriods());
        Integer b = Integer.valueOf(saveForm.getPeriodsA());
        if (a > b) {
            map.put("code", 777);
            map.put("msg", "借款期限大于合同有效期限");
            return map;
        }
        TJrProduct jrProduct = jrProductService.getById(checkdto.getProductId());
        if (jrProduct.getInstalmentPeriod() < a) {
            map.put("code", 777);
            map.put("msg", "借款期限大于金融产品期限");
            return map;
        }
        boolean checkProduct = rentInstallmentService.checkProduct(checkdto);
        if (!checkProduct) {
            map.put("code", 777);
            map.put("msg", "申请失败，超过产品期限");
            return map;
        } else {
            SaveRentInstallmentRequestDTO dto = new SaveRentInstallmentRequestDTO();
            BeanUtils.copy(saveForm, dto);
            dto.setBusinessId(Long.valueOf(44));
            //去查询是否给该商户授权了该产品
            ViewDetailsRequestDto viewDetailsRequestDto = new ViewDetailsRequestDto();
            viewDetailsRequestDto.setBusinessId(dto.getBusinessId());
            viewDetailsRequestDto.setProductId(Long.parseLong(saveForm.getProductId()));
            ZJFInformation findById = fkBusinessCreditService.findByBusinessAndProductId(viewDetailsRequestDto);
            //未给该商户授权该产品
            if (findById == null) {
                map.put("code", 777);
                map.put("msg", "授信额度不足，请联系维数公寓客服：400-008-3363");
                return map;
            }
            dto.setRate(new BigDecimal(saveForm.getRate()));
            dto.setTenantRate(new BigDecimal(saveForm.getTenantRate()));
            dto.setBusinessRate(new BigDecimal(saveForm.getBusinessRate()));
            dto.setServiceFee(new BigDecimal(saveForm.getServiceFee()));
            dto.setLoanAmount(new BigDecimal(saveForm.getBalance()));
            dto.setMonthLimit(Integer.parseInt(saveForm.getPeriods()));
            dto.setJrProductId(Long.parseLong(saveForm.getProductId()));
            dto.setContractId(Long.parseLong(saveForm.getContractId()));
            dto.setReLeaseCount(saveForm.getReLeaseCount());
            dto.setInInterestCount(saveForm.getInInterestCount());
            dto.setSerCharge(new BigDecimal(saveForm.getSerCharge()));
            dto.setCoHirerentCount(saveForm.getCoHirerentCount());
            rentInstallmentService.saveRentInstallment(dto);
        }
        map.put("code", 200);
        map.put("msg", "查询成功");
        return map;
    }


    /**
     *
     * @Description: 保存 - 分期申请
     * @return ModelAndView
     */
//    @RequestMapping("save")
//    public Map<String,Object> save(SaveRentInstallmentForm saveForm) {
//        HashMap<String, Object> map = new HashMap<>();
//        Integer a = Integer.valueOf(saveForm.getMonthLimit());
//        Integer b = Integer.valueOf(saveForm.getPeriodsA());
//        if(a > b) {
//            map.put("code",777);
//            map.put("msg","借款期限大于合同有效期限");
//            return map;
//        }
//        map.put("code",200);
//        map.put("msg","查询成功");
//        return map;
//    }
}
